package com.framework.dbs.orm.impl;

import com.framework.dbs.orm.IDialect;

public class SQLServerDialect1 implements IDialect {
	
	private String SQLSERVER_PAGEDSQL_FORMATTER;
	public SQLServerDialect1() {
		SQLSERVER_PAGEDSQL_FORMATTER="WITH A_SQLSERVER_PAGEDSQL AS(%z%)" +
				"SELECT * FROM A_SQLSERVER_PAGEDSQL WHERE RowNumber BETWEEN %y% AND %x%";
    }

    public String getPagedString(String sql, boolean hasOffset) {
        return null;
    }

    public String getPagedString(String sql, int offset, int limit) {
    	offset++;
        int endset = (offset + limit) - 1;
        String rs = SQLSERVER_PAGEDSQL_FORMATTER.replaceFirst("%z%", sql).replaceFirst("%y%", offset + "").replaceFirst("%x%", endset + "");
        return rs;
    	
    }

    static int getAfterSelectInsertPoint(String sql) {
        int selectIndex = sql.toLowerCase().indexOf("select");
        int selectDistinctIndex = sql.toLowerCase().indexOf("select distinct");
        return selectIndex + (selectDistinctIndex != selectIndex ? 6 : 15);
    }

    public boolean supportsPaged() {
        return true;
    }

}
